package val.to.UI;

import java.util.Timer;
import java.util.TimerTask;

import val.to.ErrorHelper;
import val.to.Globals;
import val.to.R;
import val.to.SessionEvents;
import val.to.SessionEvents.AuthListener;
import val.to.config.Constants;
import val.to.hardware.DeviceInfo;
import android.R.string;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;

public class EntranceActivity extends Activity {

	final int FACEBOOK_AUTHORIZE_ACTIVITY_REQUEST_CODE = 102;
	String[] mPermissions = new String[] { "publish_stream", "user_photos",
			"email" };

	private LoginButton mLoginButton;
	private Handler mHandler;
	private boolean quickUpload;
	private AlertDialog mErrorDlg;

	@Override
	protected void onPause() {
		super.onPause();
		if (mErrorDlg != null){
			mErrorDlg.dismiss();
			mErrorDlg = null;
		}
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		mHandler = new Handler();
		Globals.Init(this);
		
		SessionEvents.addAuthListener(new FacebookAuthListener());
		setContentView(R.layout.entrance);
		InitUIComponents();
		
		quickUpload = getIntent().getStringExtra("qhickUpload") != null;
		boolean afterLogout = getIntent().getStringExtra("afterLogout") != null;

		if (afterLogout){
			mLoginButton.setVisibility(View.VISIBLE);
			return;
		}
	
		try {
			DeviceInfo.getInstance().init(this);
		} catch (Exception e) {
			Log.e(Constants.TAG, "Fatal error initiating components", e);
			finish();
		}

		TimerTask task = new TimerTask() {
			@Override
			public void run() {
				if (Globals.FacebookSession.isSessionValid()) {
					// finish with nearby events activity.
					mHandler.post(new Runnable() {
						@Override
						public void run() {
							finish();

							Intent valtoIntent = new Intent()
									.setClass(EntranceActivity.this,
											EventsActivity.class);

							startActivity(valtoIntent);
						}
					});
				} else {
					// Show login button
					mHandler.post(new Runnable() {
						@Override
						public void run() {
							mLoginButton.setVisibility(View.VISIBLE);
						}
					});
				}
			}
		};

		Timer timer = new Timer();
		if (!quickUpload)
			timer.schedule(task, Constants.SPLASH_DURATION);
		else
			mLoginButton.setVisibility(View.VISIBLE);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		Globals.FacebookSession.extendAccessTokenIfNeeded(this, null);
	}

	private void InitUIComponents() {
		// connect UI components to local variables
		mLoginButton = (LoginButton) findViewById(R.id.loginButton1);
		mLoginButton.setVisibility(View.INVISIBLE);
		
		// init login (logout) button
		mLoginButton.init(this, FACEBOOK_AUTHORIZE_ACTIVITY_REQUEST_CODE, Globals.FacebookSession, mPermissions);
		
	}

	// authentication listener
	public class FacebookAuthListener implements AuthListener {
		@Override
		public void onAuthSucceed() {
			Intent valtoIntent = new Intent().setClass(EntranceActivity.this,EventsActivity.class);
			if(quickUpload){
				valtoIntent.putExtra("quickUpload", "SoMuchFUNNNN");
			}
			startActivity(valtoIntent);
			finish();
		}

		@Override
		public void onAuthFail(String error) {
			mErrorDlg = ErrorHelper.showErrorAlert(EntranceActivity.this, "Login Failed", error);
		}
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		Globals.FacebookSession.authorizeCallback(requestCode, resultCode, data);
	}
	
	@Override
	public void onBackPressed() {
		Intent intent = new Intent();
		intent.setAction(Intent.ACTION_MAIN);
		intent.addCategory(Intent.CATEGORY_HOME);
		
		startActivity(intent);
	}
}
